/*权限管理*/
import {uaredirect} from "./utils/uaredirect.js"
import router from './router'
import store from './store'
import {Message} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {getAppid, getFooter, getLoginInfo, getToken, getUuid, setAppid,setToken, removeToken} from '@/utils/auth' // get token from cookie
import {GetQueryValue, guid} from "@/utils";

uaredirect(window.location.href + 'trainZjH5/');

if (localStorage.getItem('whitelist')) {
  var whitelist = JSON.parse(store.state.permission.whitelist) ;
}else {
  var  whitelist = ['/','/login','/index','/401','/404','/play','/weak-password',"/playVideo","/course/singleCourseInfo"]
}
NProgress.configure({showSpinner: false}) // NProgress Configuration


router.beforeEach(async (to, from, next) =>  {
  var appid = getAppid();
  if (!appid) {
    var req = new XMLHttpRequest();
    req.open('GET', '/', false);
    req.setRequestHeader("Cache-Control","no-cache");
    req.onreadystatechange = function() {
      if (req.readyState === 4 && req.status === 200) {
        appid = req.getResponseHeader("api-appid");
        setAppid(appid)
      }
    };
    req.send();
  }

  if (to.fullPath !== '/404' && to.fullPath !== '/401'   && to.fullPath !== '/login' && to.fullPath !== '/playVideo') {
    const hasFooter = getFooter();
    if (hasFooter) {
      document.title = JSON.parse(hasFooter).appTitle;
    }else {
      store.dispatch('app/getFooter').then(() => {
        document.title = store.getters.footer.appTitle;
      })
    }
  }

  if (to.fullPath !== '/404' && to.fullPath !== '/401'  && to.fullPath !== '/login' && to.fullPath !== '/playVideo' && to.fullPath !== '/course/singleCourseInfo') {
    // 确定用户是否已登录
    if (to.query.token){
      store.commit('user/SET_TOKEN', to.query.token);
      store.dispatch('user/getInfo');
      setToken(to.query.token);
      next(to.path);
    }

    //必读通知
    // store.dispatch('user/getNotice',to.path)
    var accessRoutes = await store.dispatch('permission/generateRoutes');
    // 设置网页标题


    const hasToken = getToken();
    //确认用户有没有uuid
    const hasUuid = getUuid();

    store.dispatch('app/changeActiveMenu', to.path);
    if (!hasUuid) {
      store.dispatch('user/setUuid', guid())
    }

    if (hasToken) {
      const hasName = getLoginInfo('name');
      if (hasName) {
        next();
      } else {
        store.dispatch('user/getInfo');
        next();
      }
    } else {
      /* has no token*/
      const normalizedWhitelist = whitelist.map(url => new URL(url, window.location.href).pathname);
      if (normalizedWhitelist.indexOf(new URL(to.path, window.location.href).pathname) !== -1) {
        next();
      } else {
        Message.error('请先登录');
        next({path: '/index', query: {re_path: to.path, ...to.query}}) // 否则全部重定向到首页
      }
    }
  }else {
    const hasUuid = getUuid();
    if (!hasUuid) {
      store.dispatch('user/setUuid', guid())
    }
    next()
  }


})

router.afterEach(() => {

  NProgress.done()
})

